package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyServcie;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.MyFileName;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

public class CompanyServiceImpl implements ICompanyServcie {
    File file = null;

    /**
     * 数据来源于文件
     * 首先，我们需要找到Java的文件怎么处理？
     * 所以，我们需要知道Java是怎么把文件抽象到Java类的？
     * 1.创建文件？
     * 2.写入数据，既然我们的数据来源于文件，文件里面必须有数据?
     * 3.读取数据。
     * 4.封装到CompanyModel
     * 5.返回数据
     * 一个方法里面制作一件事情，低耦合
     * 将所有的方法进行调用
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModelForHomePage() {

//        1.创建文件
        IOTool.createNote(MyFileName.COMPANY_FILE_NAME);
//        2.写入数据
        CompanyDao companyDao=new CompanyDao();
//        List<String> data=companyDao.getData();
        IOTool.wirteFile(companyDao.getData());
//        3.读取数据
        String result = IOTool.readFile();
//        System.out.println(result);
//        4.封装数据
        CompanyModel companyModel = setCompanyModelData(result);
//        5.返回数据

        return companyModel;
    }

    /**
     * 根据result的数据封装到companyModel里面
     * 统一接口规范
     *
     * @param result
     * @return
     */
    private CompanyModel setCompanyModelData(String result) {
        CompanyModel companyModel = new CompanyModel();
        String[] results = result.split(MyFileName.SPLIT);
        System.out.println(Arrays.toString(results));
        companyModel.setLogo(results[0]);
        companyModel.setPhone(results[1]);
        companyModel.setNet(results[2]);
        companyModel.setEmail(results[3]);
        companyModel.setAddress(results[4]);
        companyModel.setVersion(results[5]);
        companyModel.setLinePhone(results[6]);
        return companyModel;
    }



    /**
     * 写入数据
     */
//    private void wirteFile() {
//
////            char value[] = logo.toCharArray();
////            for (char a : value
////            ) {
////                outputStream.write(a);
////            }
//        try {
//            OutputStream outputStream = new FileOutputStream(file);
//            CompanyDao companyDao = new CompanyDao();
//            String logo = companyDao.getLogo();
//            outputStream.write(logo.getBytes());
//            outputStream.write(MyFileName.SPLIT.getBytes());
//            outputStream.write(companyDao.getPhone().getBytes());
//            outputStream.write(MyFileName.SPLIT.getBytes());
////            魔法值 ，魔法值不能单独出现在某个地方，二十应该提取出来放到统一的类里面进行单独管理
//            outputStream.write(companyDao.getNet().getBytes(StandardCharsets.UTF_8));
//            outputStream.write(MyFileName.SPLIT.getBytes());
//            outputStream.write(companyDao.getEmail().getBytes());
//            outputStream.write(MyFileName.SPLIT.getBytes());
//            outputStream.write(companyDao.getAddress().getBytes());
//            outputStream.write(MyFileName.SPLIT.getBytes());
//            outputStream.write(companyDao.getVersion().getBytes());
//            outputStream.write(MyFileName.SPLIT.getBytes());
//            outputStream.write(companyDao.getLinePhone().getBytes());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//    }


}
